package com.springcloud.sc.ucpv5.init;

import com.springcloud.sc.ucp.v5.api.entity.UcpServerInfo;
import com.springcloud.sc.ucp.v5.api.entity.UcpUserDetailInfo;
import com.springcloud.sc.ucpv5.client.application.ApplicationUtil;
import com.springcloud.sc.ucpv5.client.application.message.MesssageHandle;
import com.springcloud.sc.ucpv5.client.module.datamanager.BaseDataManager;
import com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager;
import com.springcloud.sc.ucpv5.service.UcpServerInfoService;
import com.springcloud.sc.ucpv5.service.UcpUserDetailInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StartCommandRunner implements CommandLineRunner {

    @Value("${spring.serverPort}")
    private String serverPort ;
    @Value("${spring.serverIp}")
    private String serverIp ;
    @Value("${spring.Fpassword}")
    private String userPassword;
    @Value("${spring.FuserName}")
    private String userName;

    @Autowired
    private UcpUserDetailInfoService ucpUserDetailInfoService;

/*    @Autowired
    private UcpServerInfoService ucpServerInfoService;*/

    @Autowired
    private RedisTemplate redisTemplate;

    public void run(String... args) throws Exception {
        this.startStreamTaskV5();
        //准备写入Redis中的在线人数信息
        System.out.println("正在读取数据库中在线人数");
        List<UcpUserDetailInfo> ucpUserDetailInfoList = ucpUserDetailInfoService.getListAll();
        ucpUserDetailInfoList.forEach(ucpUserDetailInfo -> {
            redisTemplate.opsForValue().set( "online."+ucpUserDetailInfo.getDN() , ucpUserDetailInfo  );
        });
        System.out.println("在线人数读取完成");
    }
    public void startStreamTaskV5() {
        // 设置登录参数
        LoginDataManager.getInstance().setServerVerType(2);  // 0 表示V4 tcp连接 1 表示V4 vtcp连接 2 表示V5
        LoginDataManager.getInstance().setServerIp( serverIp );
        LoginDataManager.getInstance().setServerPort(serverPort);
        LoginDataManager.getInstance().setName( userName );
        LoginDataManager.getInstance().setPassWord( userPassword );
        ApplicationUtil.initCnmsSystem();
    }
}
